const nosocomium = require('../service/nosocomium')

// 获取医院列表
const getNosocomiumList = async (ctx, next) => {
    let res = await nosocomium.nosocomiumList(ctx.query)
    if(res.data.length > 0){
        return ctx.body={
            code:20000,
            data:res.data,
            total:res.total,
            msg:'查询成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'传递的参数有误'
        }
    }
}

// 医院详情
const getNosocomiumDetail = async (ctx, next) => {
    let res = await nosocomium.nosocomiumDetail(ctx.query)
    if(res.length > 0){
        return ctx.body={
            code:20000,
            data:res,
            msg:'查询成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'传递的参数有误'
        }
    }
}

// 医院新增
const addNosocomium = async (ctx, next) => {
    let res = await nosocomium.nosocoAdd(ctx.request.body)
    if(res){
        return ctx.body={
            code:20000,
            msg:'新增成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'新增失败'
        }
    }
}

// 医院修改
const updateNosocomium = async (ctx, next) => {
    let res = await nosocomium.nosocoUpdate(ctx.request.body)
    if(res){
        return ctx.body={
            code:20000,
            msg:'修改成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'修改失败'
        }
    }
}

// 医院删除
const deleteNosocomium = async (ctx, next) => {
    let res = await nosocomium.nosocoDelete(ctx.query)
    if(res){
        return ctx.body={
            code:20000,
            msg:'删除成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'删除失败'
        }
    }
}

// 获取文章列表
const getBookList = async (ctx)=>{
    let res = await nosocomium.getBookslist(ctx.query)
    if(res.data.length > 0){
        return ctx.body ={
            code:20000,
            data:res.data,
            total:res.total,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'数据错误'
        }
    }
}
// 获取文章详情
const getBooksDetail = async(ctx)=>{
    let res = await nosocomium.getBookDetail(ctx.query)
    if(res.length > 0){
        return ctx.body ={
            code:20000,
            data:res,
            total:res.length,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'数据错误'
        }
    }
}
// 文章阅读量
const bookRead = async(ctx)=>{
    let res = await nosocomium.bookBrowse(ctx.request.body)
    if(res.affectedRows){
        return ctx.body ={
            code:20000,
            msg:'阅读量+1'
        }
    }
}
// 文章搜索
const getBookSearch = async(ctx)=>{
    let res = await nosocomium.getBookSearch(ctx.query)
    if(res.length > 0){
        return ctx.body ={
            code:20000,
            data:res,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'数据错误'
        }
    }
}
// 文章新增
const addBook = async (ctx)=>{
    let res = await nosocomium.bookAdd(ctx.request.body)
    if(res){
        return ctx.body={
            code:20000,
            msg:'新增成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'新增失败'
        }
    }
}
// 文章修改
const updateBook = async (ctx)=>{
    let res = await nosocomium.bookUpdate(ctx.request.body)
    if(res){
        return ctx.body={
            code:20000,
            msg:'修改成功'
        }
    }else{
        return ctx.body={
            code:40001,
            msg:'修改失败'
        }
    }
}

// 删除文章
const deleteBook = async(ctx)=>{
    let res = await nosocomium.bookDelete(ctx.query)
    if(res.affectedRows){
        return ctx.body ={
            code:20000,
            msg:'删除成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'删除失败'
        }
    }
}

// 获取医生列表
const getDoctor = async (ctx)=>{
    let res = await nosocomium.getDoctorlist(ctx.query)
    if(res.data.length > 0){
        return ctx.body ={
            code:20000,
            data:res.data,
            total:res.total,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'数据错误'
        }
    }
}

// 医生详情
const getDoctorDetail = async(ctx)=>{
    let res = await nosocomium.getDoctorDetail(ctx.query)
    if(res.length > 0){
        return ctx.body ={
            code:20000,
            data:res,
            total:res.length,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'数据错误'
        }
    }
}

// 关注医生
const attentionDoctor = async(ctx)=>{
    let res = await nosocomium.attention(ctx.request.body)
    if(res == 1){
        return ctx.body ={
            code:20000,
            data:res,
            msg:'关注成功'
        }
    }else{
        return ctx.body ={
            code:20000,
            data:res,
            msg:'取消关注'
        }
    }
    
}

// 获取关注的医生列表
const getDoctorlistLike = async(ctx)=>{
    let res = await nosocomium.getDoctorlistLike(ctx.query)
    return ctx.body = {
        code:20000,
        data:res,
        total:res.length,
        msg:'查询成功'
    }
}

module.exports = {
    getNosocomiumList,
    getBookList,
    getDoctor,
    getNosocomiumDetail,
    getBooksDetail,
    getDoctorDetail,
    attentionDoctor,
    getDoctorlistLike,
    deleteNosocomium,
    addNosocomium,
    updateNosocomium,
    deleteBook,
    bookRead,
    getBookSearch,
    addBook,
    updateBook
}